//
//  XMGEssenceViewController.m
//  百思不得姐
//
//  Created by 杨庆 on 2016/12/30.
//  Copyright © 2016年 杨庆. All rights reserved.
//

#import "XMGEssenceViewController.h"
#import "XMGRecommendTagsController.h"

///**5个子控制器*/
//#import "XMGVideoViewController.h"
//#import "XMGAllViewController.h"
//#import "XMGWordViewController.h"
//#import "XMGVoiceViewController.h"
//#import "XMGPictureViewController.h"

@interface XMGEssenceViewController ()<UIScrollViewDelegate>
@property(nonatomic,strong)UIView *indicatorView;
/**当前选中的按钮*/
@property(nonatomic,weak)UIButton*currentSelectBtn;
/**顶部所有标签*/
@property(nonatomic,weak)UIView*titleView;
/**底部所有内容*/
@property(nonatomic,weak)UIScrollView*contentView;
@end

@implementation XMGEssenceViewController

- (void)viewDidLoad {
    [super viewDidLoad];

	//设置导航栏
	[self setupNav];
	
	//初始化子控制器
	[self setupChildVces];
	
	//设置顶部标签栏
	[self setupTitleView];
	
	//底部的 scrollerView
	[self setupContentView];
	
	
}

/**
 *初始化子控制器
 */
-(void)setupChildVces{
	//所有
	XMGTopiceViewController*all=[[XMGTopiceViewController alloc]init];
	all.title=@"全部";
	all.type=XMGTopicTypeAll;
	
	[self addChildViewController:all];
	//视频
	XMGTopiceViewController*video=[[XMGTopiceViewController alloc]init];
	video.title=@"视频";
	video.type=XMGTopicTypeVideo;

	[self addChildViewController:video];
	//声音
	XMGTopiceViewController*voice=[[XMGTopiceViewController alloc]init];
	voice.title=@"声音";
	voice.type=XMGTopicTypeVoice;
	[self addChildViewController:voice];
	//图片
	XMGTopiceViewController*picture=[[XMGTopiceViewController alloc]init];
	picture.title=@"图片";
	picture.type=XMGTopicTypePicture;
	[self addChildViewController:picture];
	//段子
	XMGTopiceViewController*word=[[XMGTopiceViewController alloc]init];
	word.title=@"段子";
	word.type=XMGTopicTypeWord;
	[self addChildViewController:word];

}

/**底部 scrollerView*/
-(void)setupContentView{
	//不要自动调整 inset
	self.automaticallyAdjustsScrollViewInsets=NO;
	
	UIScrollView*contentView=[[UIScrollView alloc]init];
	//设置scrollerView的代理
	contentView.delegate=self;
	contentView.pagingEnabled = YES;
	contentView.frame=self.view.bounds;
	contentView.pagingEnabled=YES;
	[self.view insertSubview:contentView atIndex:0];
	contentView.contentSize=CGSizeMake(contentView.width*self.childViewControllers.count, 0);
		self.contentView=contentView;
	
	// 添加第一个控制器的view
	[self scrollViewDidEndScrollingAnimation:contentView];
}



//设置顶部标签栏
-(void)setupTitleView{

	//标签栏整体
	UIView*titleView=[[UIView alloc]init];
	titleView.backgroundColor=[[UIColor whiteColor] colorWithAlphaComponent:0.5];
	titleView.width=self.view.width;
	titleView.height=35;
	titleView.y=64;
	[self.view addSubview:titleView];
	self.titleView=titleView;
	//底部红色指示器
	UIView*indicatorView=[[UIView alloc]init];
	indicatorView.backgroundColor=[UIColor redColor];
	indicatorView.height=2;
	indicatorView.y=titleView.height-indicatorView.height;
	self.indicatorView=indicatorView;
	
	//内部子标签
	CGFloat width=titleView.width/self.childViewControllers.count;
	CGFloat height=titleView.height;
	for (NSInteger i =0 ; i<self.childViewControllers.count ; i++) {
		UIViewController*vc=self.childViewControllers[i];
		UIButton*button=[UIButton buttonWithType:UIButtonTypeCustom];
		button.tag=i;
		button.width=width;
		button.height=height;
		button.x=i * width;
		[button setTitle: vc.title forState:UIControlStateNormal];
		[button setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
		[button setTitleColor:[UIColor redColor] forState:UIControlStateDisabled];
		button.titleLabel.font=[UIFont systemFontOfSize:14];
	
		//添加按钮监听
		[button addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
		[titleView addSubview:button];
		
		//默认点击第一个按钮
		if(i==0){
			button.enabled=NO;
			self.currentSelectBtn=button;
			//让按钮内部 label 根据内容设置尺寸
			[button.titleLabel sizeToFit];
			self.indicatorView.width=button.titleLabel.width;
			self.indicatorView.centerX=button.centerX;
		}
	}
	//保证按钮先加入
	//加入指示器
	[titleView addSubview:indicatorView];

	
}


//标签栏按钮的点击
-(void)btnClick:(UIButton*)button{
	
	self.currentSelectBtn.enabled=YES;
	button.enabled=NO;
	self.currentSelectBtn=button;
	
	//动画
	[UIView animateWithDuration:0.24 animations:^{
		self.indicatorView.width=button.titleLabel.width;
		self.indicatorView.centerX=button.centerX;
	}];
	
	
	//滚动
	CGPoint offset =self.contentView.contentOffset;
	offset.x=button.tag*self.contentView.width;
	[self.contentView setContentOffset:offset animated:YES];
	

	
}



//初始化导航栏
-(void)setupNav{
	
	//设置导航栏标题
	self.navigationItem.titleView =[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"MainTitle"]];
	
	//设置导航栏左边的按钮
	UIBarButtonItem*essenceItem=[UIBarButtonItem itemWithImage:@"MainTagSubIcon" highImage:@"MainTagSubIconClick" target:self action:@selector(tagClick)];
	self.navigationItem.leftBarButtonItem=essenceItem;
	//设置背景色
	self.view.backgroundColor= XMGColorbg;

}

-(void)tagClick{
	//点击按钮 push 控制器
	XMGRecommendTagsController*tags=[[XMGRecommendTagsController alloc]init];
	[self.navigationController pushViewController:tags animated:YES];
}
#pragma mark-<scrollViewDelegate>
//自动滚动 --动画结束
-(void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView{
	//当前索引
	NSInteger index=scrollView.contentOffset.x/scrollView.width;
	//取出子控制器
	UITableViewController*vc=self.childViewControllers[index];
	vc.view.x=scrollView.contentOffset.x;
	vc.view.y=0;
	vc.view.height=scrollView.height;
		
	[scrollView addSubview:vc.view];
}

//手动滚动
/**停止减速*/
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{

	[self scrollViewDidEndScrollingAnimation:scrollView];
	
//	//点击按钮
	NSInteger index= scrollView.contentOffset.x/scrollView.width;
	[self btnClick:self.titleView.subviews[index]];
}
@end
